/*
 *供应报盘
 *@authorFZH 2015-1027
 */
$(function() {

	var defaultPagingObj = {
		total: 0,
		pageSize: 7,
		page: 1
	};

	// 渲染页面 通用部分-头部，底部;
	commTemplUtil.initPage();

	// 渲染页面 配置数据--二级分类, 排序, 省市;
	renderConfigData();
	bindEvent();
	getGoodsListData(1);

	// 设置头部导航栏
	commTemplUtil.setHeadCommNav(5);

	//初始化 热销列表
	hotList();
	//初始化 推荐供应商列表
	groomsupply();
});

function renderConfigData() {

	// 页面大广告,main-ad-img
	$("#main-ad-img").attr("src", PAGE_DATA.main_ad_img || "");

	// 主要分类：咖啡豆及咖啡相关,等.
	commTemplUtil.initCommCategMenu("main-tab", PAGE_DATA.main_catog_data || []);
};

/**
 * 获取 商品列表<br/>
 * @author  fsl-gongl-20151027
 */
function getGoodsListData(page) {

	var onClickObj = {
		onClick: function() {
			var clickPage = $(this).attr("data-page");
			getGoodsListData(parseInt(clickPage));
		}
	};

	var listTempl = _.template($("#enquiry-list-templ").html())
	var listArea = $("#enquiry_list_all");
	// 数据为 0 时;
	var listHtmlEmpty = listTempl([]);

	var timeVal = $("#sel_time").val() || "";
	var addrVal = $("#sel_addr").val() || "";
	var goodName = $("#search-good").val() || "";
	var goodTyp = $("#good-type").attr("value") || "";

	$.ajax({
		url: PAGE_CONFIG.requestUrl.buyerList,
		type: "get",
		dataType: "json",
		data: {
			page: page,
			pageSize: PAGE_DATA.pagingObj.pageSize,
			goodType: goodTyp,
		},
		success: function(result) {

			var listData = $.extend([], result.data);
			var pageConf = $.extend(result.pagingConfData, onClickObj);

			if (listData.length == 0) {

				listArea.html([]);
			}

			var listHtml = listTempl(listData);
			listArea.html(listHtml);
			commTemplUtil.initPaging("#comm-paging", pageConf);
			//登录状态的才操作
			var getLoginState = COMM_DATA.topObj.loginFlag;
			if (getLoginState == "1") {
				//获取供应商 采购商 类型
				var getSo = COMM_DATA.topObj.createid;
				if (getSo == "2") {
					$(".enquiry-btn").click(function() {
						alert("采购商不能报盘");
					});

				} else {
					//弹出框
					createDialog();
				}

			} else {
				$(".enquiry-btn").click(function() {
					location.href = "" + COMM_DATA.topObj.login_href + "";
				});
			}
		},
		error: function(xhr, status, error) {
			listArea.html([]);
		}
	});

};

/**
 * 二级分类, 搜索, 绑定一次;
 */
function bindEvent() {

	$.commNavTabs2($("#main-tab").find(".tab-head").find(".text"),
		$("#main-tab").find(".tab-main").find("ul"),
		"active");

	// 商品分类 搜索用 id;
	var goodType = $("#good-type");

	// 一级分类
	$("#tab-head-box").find(".text").on("click", function() {

		var searchVal = $(this).attr("value");
		goodType.val($(this).attr("value"));
		getGoodsListData(1);

	});

	goodType.val($("#tab-head-box").find(".text").eq(0).attr("value"));

	// 二级分类
	$("#tab-detail-box").find("ul").each(function() {

		var thatUl = $(this);
		var siblingUlLink = $(this).siblings().find("a");

		thatUl.find("li").on("click", function() {

			$(this).find("a").addClass("current").end().siblings().find("a").removeClass("current");
			siblingUlLink.removeClass("current");
			goodType.val($(this).attr("value"));

			getGoodsListData(1);

		});
	});
	// 搜索
	$("#search-btn").on("click", function() {
		getGoodsListData(1);
	});
};
//弹出框的加载
function createDialog() {
	var tmplObj = _.template($('#supply_offer_templ').html());
	var tmplObjHtml = tmplObj();
	$(".enquiry-btn").click(function() {
		var getCode = $(this).attr("code");
		var getMax = $(this).attr("maxSupply");
		var dia = dialog({
			id: 'offer-info',
			title: ' ',
			content: tmplObjHtml,
			onshow: function() {
				//弹出框的验证
				var formVal = $("[id='content:offer-info']").find("#supper_offer_sub").validate({
					rules: {
						price: {
							required: true,
							number: true,
						},
						amount: {
							required: true,
							digits: true,
						},
						contact_name: "required",
						contact_phone: {
							required: true,
							mobileCHINA: true,
						},
						county_id: "required",
					},
					messages: { // 
						contact_phone: {
							required: "请输入手机号码",
							mobileCHINA: "请输入有效手机号码"
						},
						amount: {
							digits: "只能输入整数",
						},
					},
					errorPlacement: function(error, element) {
						if (element.attr("name") == "signflag") {
							element.parents(".electronic-bottom").append(error);
						} else {
							element.parents(".enquiry").after($("<div class='key-lbl tc' style='width:auto;'/>").append(error));
						}
					},
				});
				$("#tip_alert").click(function() {
					if (formVal.form()) {

						alert("提交成功");
					}

				});

			}
		});
		dia.showModal();
		// $("#tip_alert").click(function(){
		// 	alert("提交成功");
		// });

		$("#enquiry_id").attr("value", getCode);
		$("#max_enquiry").attr("value", getMax);
		//弹出框 地区的加载
		var addrTemplObj = _.template($('#addr-option-templ').html());
		renderPage();

		function renderPage() {
			// 省
			$.ajax({
				url: PAGE_CONFIG.requestUrl.addrProvince,
				type: 'get',
				dataType: 'json',
				success: function(result) {
					var datas = $.extend([], result.data);
					var tmplObjHtml = addrTemplObj(datas);
					$("#province").html(tmplObjHtml);
				},
				error: function(xhr, status, error) {
					var tmplObjHtml = addrTemplObj([]);
					$("#province").html(tmplObjHtml);
				}
			});

			$("#province").on("change", function () {
			    var code = $(this).val();
			    renderAddrCity(code);
			});

			$("#city").on("change", function () {
			    var code = $(this).val();
			    renderAddrCounty(code);
			});
            
			function renderAddrCity(provinceCode) {

				// 市
				$.ajax({
					url: PAGE_CONFIG.requestUrl.addrCity,
					type: 'post',
					dataType: 'json',
					data: {
						code: provinceCode,
						_token: COMM_DATA.csrfToken,
					},
					success: function(result) {
						var datas = $.extend([], result.data);
						var tmplObjHtml = addrTemplObj(datas);
						$("#city").html(tmplObjHtml);

					},
					error: function(xhr, status, error) {
						var tmplObjHtml = addrTemplObj([]);
						$("#city").html(tmplObjHtml);
					}
				});

				$("#city").empty().html(addrTemplObj([]));
				$("#county").empty().html(addrTemplObj([]));
			};

			function renderAddrCounty(cityCode) {
				// 区/县
				$.ajax({
					url: PAGE_CONFIG.requestUrl.addrCounty,
					type: 'post',
					dataType: 'json',
					data: {
						code: cityCode,
						_token: COMM_DATA.csrfToken,
					},
					success: function(result) {
						var datas = $.extend([], result.data);
						var tmplObjHtml = addrTemplObj(datas);
						$("#county").html(tmplObjHtml);

					},
					error: function(xhr, status, error) {
						var tmplObjHtml = addrTemplObj([]);
						$("#county").html(tmplObjHtml);
					}
				});
			};
		};
	});

};
//热销列表

function hotList() {
	var hotShopTempl = _.template($("#hot_shop_templ").html())
	$.ajax({
		url: PAGE_CONFIG.requestUrl.buyerHotShop,
		type: "get",
		dataType: "json",
		success: function(result) {
			var datas = $.extend([], result.data);
			var hotlistHtml = hotShopTempl(datas);
			$("#hot_list").html(hotlistHtml);
		},
		error: function(xhr, status, error) {
			var hotlistHtml = hotShopTempl([]);
			$("#hot_list").html(hotlistHtml);
		},

	});
};
//推荐商家
function groomsupply() {
	var hotShopTempl = _.template($("#groomsupply_templ").html())
	$.ajax({
		url: PAGE_CONFIG.requestUrl.buyerGroomSupply,
		type: "get",
		dataType: "json",
		success: function(result) {
			var datas = $.extend([], result.data);
			var hotlistHtml = hotShopTempl(datas);
			$("#groomsupply_list").html(hotlistHtml);
		},
		error: function(xhr, status, error) {
			var hotlistHtml = hotShopTempl([]);
			$("#groomsupply_list").html(hotlistHtml);
		},

	});
};